package com.my.study.reflection;

import com.my.study.reflection.chart.AbstractChart;
import com.my.study.reflection.chart.IChart;
import com.my.study.reflection.utils.Resolver;
import lombok.extern.slf4j.Slf4j;
import org.junit.Test;
import org.reflections.Reflections;
import org.reflections.util.ConfigurationBuilder;
import org.reflections.util.Utils;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

/**
 * @author: yidujun
 * @create: 2021/07/15 15:56
 */
@Slf4j
public class ReflectionTest {

    private static final String PACKAGE_PATH = "com.my.study.reflection.chart";

    // =================================测试Reflections包工具类====================================

    @Test
    public void testReflections() {
        ConfigurationBuilder builder = new ConfigurationBuilder()
                .forPackages(PACKAGE_PATH);

        Reflections reflections = new Reflections(builder);

        Set<Class<? extends IChart>> sonClazz = reflections.getSubTypesOf(IChart.class);
        sonClazz.forEach(System.out::println);
    }

    @Test
    public void testUtils() {
        String ss = Utils.repeat("[]", 3);
        System.out.println(ss);
    }

    // =================================测试模仿mybatis反射工具类====================================

    public static void main(String[] args) {
        Set<Class<?>> classSet = ResolverUtils.find(IChart.class, ResolverUtils.Option.IMPLEMENT, "com.my.study.reflection.source");
        System.out.println(classSet.size());
        for (int i = 0; i < 1000; i++) {
            new Thread(() -> {
                Set<Class<?>> set = ResolverUtils.find(IChart.class, ResolverUtils.Option.IMPLEMENT, "com.my.study.reflection.chart");
                System.out.println(set.size());
            }, i + "").start();
        }
    }

    @Test
    public void testImplements() {
        Set<Class<?>> classSet = ResolverUtils.find(IChart.class, ResolverUtils.Option.IMPLEMENT, "com.my.study.reflection.chart.impl", "com.my.study.reflection.chart.impl2");
        System.out.println(classSet);

        Set<Class<?>> impl = ResolverUtils.findImpl(IChart.class, "com.my.study.reflection.chart.impl", "com.my.study.reflection.chart.impl2");
        System.out.println(impl);
    }

    // =======注解============

    @Test
    public void testAnnotation() {
        Set<Class<?>> annotation = ResolverUtils.findAnnotation(IsPass.class, "com.my.study.reflection.chart");

        System.out.println(annotation);
    }



    @Test
    public void testAbstracts() {
        Set<Class<?>> classSet = ResolverUtils.find(AbstractChart.class, ResolverUtils.Option.IMPLEMENT, "com.my.study.reflection.chart.impl", "com.my.study.reflection.chart.impl2");
        System.out.println(classSet);
    }

    @Test
    public void testAddHeadList() {
        List<String> list = new ArrayList<>();

        list.add(0, "c");
        list.add(0, "b");
        list.add(0, "a");

        System.out.println(list);
    }

    @Test
    public void testFileLevel() {
        String path = "D:/workspace";
        File file = new File(path);

        boolean directory = file.isDirectory();
        System.out.println(directory);
    }


}
